﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tools.Domain.Specifications;
using Web.App.Infrastructure.ObjectStore.Contracts;

namespace Web.App.Infrastructure.ObjectStore.Implementation
{
    public class ListBasedObjectStore : IObjectStoreRepository
    {
        private IList<ObjectStoreItem> items;

        public ListBasedObjectStore(IList<ObjectStoreItem> items)
        {
            this.items = items;
        }

        public void Add(ObjectStoreItem item)
        {
            items.Add(item);
        }

        public ObjectStoreItem Add(object data, string description)
        {
            var newItem = new ObjectStoreItem(Guid.NewGuid(), data, DateTime.Now, data.GetType(), description);
            Add(newItem);
            return newItem;
        }

        public void Update(Guid id, object data)
        {
            var item = items.Where(x => x.Id == id).First();
            item.Data = data;

        }

        public void Remove(ObjectStoreItem item)
        {
            items.Remove(item);
        }

        public void Remove(Guid id)
        {
            throw new System.NotImplementedException();
        }

        public IEnumerable<ObjectStoreItem> Find(Func<ObjectStoreItem, bool> predicate)
        {
            return items.Where(predicate);
        }

        public IEnumerable<ObjectStoreItem> Find(ISpecification<ObjectStoreItem> predicate)
        {
            throw new NotImplementedException();
        }

        public ObjectStoreItem Find(Guid id)
        {
            return items.Where(item => item.Id == id).FirstOrDefault();
        }
    }
}
